package com.chinadci.smartcommunitysystembackstage.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinadci.smartcommunitysystembackstage.entity.ScsUser;
import com.baomidou.mybatisplus.extension.service.IService;
import com.chinadci.smartcommunitysystembackstage.mapper.ScsAdmintypeMapper;
import com.chinadci.smartcommunitysystembackstage.mapper.ScsUserMapper;
import com.chinadci.smartcommunitysystembackstage.util.MD5;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author Code Duck
 * @since 2021-04-13
 */
@Service
public class ScsUserService{
    @Autowired
    private ScsUserMapper mapper;

    private Logger log= LoggerFactory.getLogger(this.getClass());
    //显示所有用户信息
    public List<ScsUser> UserFind(String name){
        log.info("=====显示所有用户账号信息");
        List<ScsUser>list=new ArrayList<>();
        if (name!=null && name!=""){
            list=mapper.UserFind("%"+name+"%");
        }else{
            list=mapper.UserFind("%%");
        }
        return list;
    }

    //修改查询用户信息
    public ScsUser Find(int id){
        log.info("=====修改查询用户账号信息");
        QueryWrapper<ScsUser> qw=new QueryWrapper<>();
        qw.eq("u_id",id);
        return mapper.selectOne(qw);
    }
    //修改用户信息
    public int Update(ScsUser user){
        log.info("=====修改用户账号信息");
        return mapper.updateById(user);
    }
    //删除用户
    public int Del(int id){
        log.info("=====删除用户账号信息");
        QueryWrapper<ScsUser> qw=new QueryWrapper<>();
        qw.eq("u_id",id);
        return mapper.delete(qw);
    }
    //新增用户
    public int Insert(ScsUser user){
        log.info("=====新增用户账号信息");
        String pwd= MD5.getMD5(user.getUPwd(),18);
        user.setUPwd(pwd);
        return mapper.insert(user);
    }

    //新增的时候根据电话号判断
    public ScsUser Find(String phone){
        log.info("=====新增用户账号信息的时候根据手机号判断该用户是否注册过");
        QueryWrapper<ScsUser> qw=new QueryWrapper<>();
        qw.eq("u_phone",phone);
        return mapper.selectOne(qw);
    }

    //根据业主姓名查id
    public ScsUser Finds(String name){
        log.info("=====根据业主姓名查id");
        QueryWrapper<ScsUser> qw=new QueryWrapper<>();
        qw.eq("u_name",name);
        return mapper.selectOne(qw);
    }


}
